package com.bstek.uflo.form.view.table.dialect.impl;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import org.apache.commons.lang.StringUtils;

import com.bstek.uflo.form.view.table.dialect.Dialect;

public abstract class AbstractDialect implements Dialect {

	/**
	 * 判断是否是支持的数据库类型
	 * @param connection 数据连接
	 * @param dbProductName 数据库名称
	 * @param dbMajorVersion 数据版本号
	 * @return 返回当前方言是否支持当前数据库
	 */
	public boolean support(Connection connection,String dbProductName,String dbMajorVersion) {
		try {
			DatabaseMetaData databaseMetaData = connection.getMetaData();
			String databaseProductName = databaseMetaData.getDatabaseProductName();
			int databaseMajorVersion = databaseMetaData
			.getDatabaseMajorVersion();
			boolean containsMysql = StringUtils.containsIgnoreCase(databaseProductName,dbProductName);
			if(StringUtils.isNotEmpty(dbMajorVersion)){
				return containsMysql&&Integer.valueOf(dbMajorVersion)==databaseMajorVersion;
			}
			return containsMysql;
		} catch (SQLException e) {
			return false;
		}
	}
}
